//
//  LeLeHomeVC.m
//  xiaoshuoapp
//
//  Created by fxcc on 2021/6/18.
//

#import "LeLeHomeVC.h"
#import <SafariServices/SFFoundation.h>
#import <SafariServices/SFSafariViewController.h>
#import "RootViewController.h"
#import <AdSupport/AdSupport.h>
#import <Foundation/Foundation.h>
#import "ZGCNetWork.h"
#import "SplashAdModel.h"
#import "NSObject+YJModelDicTransform.h"
#import "AdvertiseView.h"
@interface LeLeHomeVC ()<MobSplashAdManagerDelegate>
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong)SplashAdModel*model;
@end

@implementation LeLeHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [UIColor colorWithPatternImage:[self launchImageWithType:@"Portrait"]];
    [self launchADView];
//    [self splashViewDismissScreen];
    // Do any additional setup after loading the view.
}
-(UIImage *)launchImageWithType:(NSString *)type{
    //比对分辨率,获取启动图 fix #158:https://github.com/CoderZhuXH/XHLaunchAd/issues/158
    CGFloat screenScale = [UIScreen mainScreen].scale;
    CGSize screenDipSize = CGSizeMake([UIScreen mainScreen].bounds.size.width * screenScale, [UIScreen mainScreen].bounds.size.height * screenScale);
    NSString *viewOrientation = type;
    NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
    for (NSDictionary* dict in imagesDict){
        UIImage *image = [UIImage imageNamed:dict[@"UILaunchImageName"]];
        CGSize imageDpiSize = CGSizeMake(CGImageGetWidth(image.CGImage), CGImageGetHeight(image.CGImage));
        if([viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]]){
            if([dict[@"UILaunchImageOrientation"] isEqualToString:@"Landscape"]){
                imageDpiSize = CGSizeMake(imageDpiSize.height, imageDpiSize.width);
            }
            if(CGSizeEqualToSize(screenDipSize, imageDpiSize)){
                return image;
            }
        }
    }
    return nil;
}
-(void)launchADView
{
//    [self splashViewDismissScreen];
//    [self.splashAd loadAd];
    [ZGCNetWork getAdUrl:@"http://39.107.204.92:8081/image/getguanggao" Parameter:nil andData:^(Result *result) {
        if (result.status == XTBDRequestDataOK) {
            NSDictionary * dataDic = (NSDictionary*)result.dataDic;
            NSLog(@"dataDic======%@",dataDic);
            if ([dataDic.allKeys count]>0) {
                // 读
                self.model = [SplashAdModel yj_initWithDictionary:[[dataDic objectForKey:@"data"] firstObject]];
               
                self.model.imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.model.imgurl]];
                AdvertiseView *advertiseView = [[AdvertiseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
                advertiseView.filePath = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.model.imgurl]];
                advertiseView.delegate = self;
                [advertiseView show];
               

            }else{

                [self splashViewDismissScreen];
            }

        }
        if (result.status == XTBDRequestDataError) {
            [self splashViewDismissScreen];
        }
        if (result.status == XTBDRequestDataTimeOut) {
            [self splashViewDismissScreen];
        }
    }];
    
    
}
- (void)splashViewShowSuccess{
    
}


/**
 *  开屏广告被点击后调用
 */
- (void)splashViewToClicked{
    
}

/**
 *  开屏广告展示结束时调用
 */
- (void)splashViewDismissScreen{
    RootViewController * con =[[RootViewController alloc]init];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = con;
}

/**
 *  开屏广告详情页消失时调用
 */
- (void)splashViewDidDismissLp{
    
}

/**
 *  开屏广告曝光成功时调用
 */
- (void)splashViewToExposure{
  
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
